package com.leixiaoqiao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;//pagehelper的专有类
import com.leixiaoqiao.dao.CustomerDao;
import com.leixiaoqiao.dao.impl.CustomerDaoImpl;
import com.leixiaoqiao.pojo.Customer;
import com.leixiaoqiao.service.CustomerService;
import com.leixiaoqiao.util.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao;
    public CustomerServiceImpl(){
        customerDao = new CustomerDaoImpl();
    }
    //业务层实现类 : 查询不需要事务、增、删除、修改需要事务支持
    @Override
    public void addCustomer(Customer customer) {
       try {
           customerDao.addCustomer(customer);
           //释放资源提交事务
           SqlSessionUtil.close();
       }catch (Exception e){
           e.printStackTrace();
           SqlSessionUtil.rollBack();
       }
    }

    @Override
    public void deleteCustomerById(int id) {
        try {
            customerDao.deleteCustomerById(id);
            SqlSessionUtil.close();
        }catch (Exception e){
            e.printStackTrace();
            SqlSessionUtil.rollBack();
        }
    }

    //批量删除
    @Override
    public void deleteCustomers(String[] ids) {
        if (ids!=null){
            try {
                for (int i = 0; i <ids.length ; i++) {
                    customerDao.deleteCustomerById(Integer.parseInt(ids[i]));
                }
                SqlSessionUtil.close();
            }catch (Exception e){
                e.printStackTrace();
                SqlSessionUtil.rollBack();
            }
        }
    }
    //改
    @Override
    public void updateCustomer(Customer customer) {
        try {
            customerDao.updateCustomer(customer);
            SqlSessionUtil.close();
        }catch (Exception e){
            e.printStackTrace();
            SqlSessionUtil.rollBack();
        }
    }
    //单个查询
    @Override
    public Customer findCustomerById(int id) {
        Customer customerById = customerDao.findCustomerById(id);
        SqlSessionUtil.close();
        return customerById;
    }
    //待条件的批量查询
    @Override
    public PageInfo<Customer> findPage(HashMap<String,String> queryParamMap) {
        PageHelper.startPage(Integer.parseInt(queryParamMap.get("pageNo")),
                Integer.parseInt(queryParamMap.get("pageSize")));

        //将从前端传来的查询参数map中挑选需要更改的参数，封装到数据持久层执行sql语句时需要的参数map中
        //新建数据持久层需要用到的map
        //---------------------调整封装前端数据---------------------------
        HashMap<String, Object> queryMap = new HashMap<>();
        String name = queryParamMap.get("name");
        if (!(Objects.isNull(name) || "".equals(name))){
            queryMap.put("name","%"+name+"%");
        }
        String idnumber = queryParamMap.get("idnumber");
        if (!(Objects.isNull(idnumber) || "".equals(idnumber))){
            queryMap.put("idnumber","%"+idnumber+"%");
        }
        String city = queryParamMap.get("city");
        if (!(Objects.isNull(city) || "".equals(city))){
            queryMap.put("city","%"+city+"%");
        }
        String phone = queryParamMap.get("phone");
        if (!(Objects.isNull(phone) || "".equals(phone))){
            queryMap.put("phone","%"+phone+"%");
        }
        String email = queryParamMap.get("email");
        if (!(Objects.isNull(email) || "".equals(email))){
            queryMap.put("email","%"+email+"%");
        }
        String sex = queryParamMap.get("sex");
        if (!(Objects.isNull(sex) || "".equals(sex))){
            queryMap.put("sex",sex);
        }
        //-------------------------------------------------------------
        //获取当前页的数据集合并将其封装到pageInfo中
        List<Customer> list = customerDao.findList(queryMap);
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        //释放资源
        SqlSessionUtil.close();
        return pageInfo;
    }
}
